package yanyan.com.tochar.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import yanyan.com.tochar.GifActivity;
import yanyan.com.tochar.GifToCharActivity;
import yanyan.com.tochar.MoreActivity;
import yanyan.com.tochar.MoreImageToImgActivity;
import yanyan.com.tochar.OneActivity;
import yanyan.com.tochar.QqCardActivity;
import yanyan.com.tochar.QrCodeActivity;
import yanyan.com.tochar.R;
import yanyan.com.tochar.VideoTocharActivity;
import yanyan.com.tochar.adpter.HomeListAdapter;
import yanyan.com.tochar.beans.BannerInfo;
import yanyan.com.tochar.beans.HomeListBean;
import yanyan.com.tochar.funsactivity.FunsNoticeActivity;
import yanyan.com.tochar.loader.GlideImageLoader;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.ToastUtil;


public class Home extends Fragment implements OnBannerListener {
    private  View view;
    private Context mContext;
    private Banner banner;
    private List<String> imagePaths;
    private List<String>titles;

    private GridView gridView;

    private List<HomeListBean> listBeans;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = (View) inflater.inflate(R.layout.main_home, container, false);
        banner = (Banner) view.findViewById(R.id.banner);

        gridView=view.findViewById(R.id.home_grid);
        //轮播
        initBanner();
        intGridView();
        return view;
    }

    public void intGridView(){
        listBeans=new ArrayList<>();
        HomeListBean b1=new HomeListBean(R.drawable.b1,"静态图转字符图",
                "将自己的单张图转为字符图",Color.RED,new OneActivity());

        HomeListBean b2=new HomeListBean(R.drawable.b1,"多图转动态字符图",
                "多张图片合成一张动态的字符图",Color.RED,new MoreActivity());

        HomeListBean b3=new HomeListBean(R.drawable.b1,"动态图转字符图",
                "动态图片转为动态字符图",Color.RED,new GifToCharActivity());

        HomeListBean b4=new HomeListBean(R.drawable.b1,"多图转动态图",
                "多张图片合成一张GIF动态图",Color.RED,new GifActivity());

        HomeListBean b5=new HomeListBean(R.drawable.b1,"个性二维码",
                "不一样的二维码",Color.RED,new QrCodeActivity());

        HomeListBean b6=new HomeListBean(R.drawable.b1,"视频转字符图",
                "将视频转字符视频",Color.RED,new VideoTocharActivity());

        HomeListBean b7=new HomeListBean(R.drawable.b1,"QQ装X名片",
                "QQ装X名片",Color.RED,new QqCardActivity());

        HomeListBean b8=new HomeListBean(R.drawable.b1,"模拟通知",
                "模拟手机中的APP向自己发送通知",Color.RED,new FunsNoticeActivity());

        HomeListBean b9=new HomeListBean(R.drawable.b1,"test",
                "tttttttttttt",Color.RED,new MoreImageToImgActivity());
        listBeans.add(b9);
        listBeans.add(b1);
        listBeans.add(b2);
        listBeans.add(b3);
        listBeans.add(b6);
        listBeans.add(b4);
        listBeans.add(b7);
        listBeans.add(b8);
        listBeans.add(b5);


        HomeListAdapter adapter=new HomeListAdapter(mContext,listBeans);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeListBean listBean = listBeans.get(position);
                if(listBean.getActivity()!=null){
                    if(listBeans.get(position).getActivity().getClass()==null){
                        ToastUtil.showLongToast(mContext,"请升级软件到最新版本");
                    }else {
                        Intent in=new Intent();
                        in.setClass(mContext,listBeans.get(position).getActivity().getClass());
                        startActivity(in);
                    }
                }
            }
        });


    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    /**
     * 轮播图
     */
    private void initBanner() {
        WindowManager windowManager= (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        Integer viewWidth  = dm.widthPixels ;
        banner = (Banner) view.findViewById(R.id.banner);
        ViewGroup.LayoutParams params=banner.getLayoutParams();
        params.height=viewWidth/2;
        banner.setLayoutParams(params);
        imagePaths = new ArrayList<>();
        titles = new ArrayList<>();
        if(CoreUtil.bannerInfo!=null && CoreUtil.bannerInfo.size()>0){
            for (BannerInfo info:CoreUtil.bannerInfo){
                imagePaths.add(info.getImage());
                titles.add(info.getUrl());
            }
        }

        banner.setImageLoader(new GlideImageLoader());
        banner.setBannerAnimation(Transformer.Default);
        banner.setDelayTime(3000);
        banner.isAutoPlay(true);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImages(imagePaths);
        banner.setOnBannerListener(this);
        //  banner.setOnBannerListener(this);
        banner .start();


    }


    @Override
    public void OnBannerClick(int position) {
        try{
            if(CoreUtil.bannerInfo!=null && CoreUtil.bannerInfo.size()>0){
                BannerInfo info = CoreUtil.bannerInfo.get(position);
                if(info!=null){
                    if(info.getAct()!=null && info.getAct().length()>0){
                        Intent in=new Intent();
                        in.setClass(mContext,Class.forName("yanyan.com.tochar."+info.getAct()));
                        startActivity(in);
                    }else {
                        Uri uri=Uri.parse("https://www.baidu.com/");
                        if(position<=titles.size()){
                            uri=Uri.parse(titles.get(position));
                        }
                        Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                        startActivity(intent);
                    }
                }
            }

        }catch (Exception e){
              e.printStackTrace();
        }

    }
}

